package com.mall.sun.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mall.sun.exception.Errors;
import com.mall.sun.model.dto.AgentDTO;
import com.mall.sun.model.dto.GoodAgentDTO;
import com.mall.sun.model.dto.GoodSupplierDTO;
import com.mall.sun.model.dto.PageHelper;
import com.mall.sun.model.po.Agent;
import com.mall.sun.model.vo.ResultVO;
import com.mall.sun.service.AgentService;
import com.mall.sun.service.GoodsService;
import com.mall.sun.utils.CommonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.method.P;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * description:  <br>
 * date: 2021/12/11 20:14 <br>
 * author: ztz <br>
 * version: 1.0 <br>
 */
@RestController
@RequestMapping("/agent")
public class AgentController {
    @Autowired
    AgentService agentService;
    @Autowired
    GoodsService goodsService;
    @PutMapping("/update")
    public ResultVO updateAgent(@RequestBody GoodAgentDTO goodAgentDTO){
        try {
            return CommonUtil.ajaxReturn(agentService.updateAgent(goodAgentDTO)) ;
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return CommonUtil.errorReturn(Errors.Update_Agent_Error);
    }
    @PreAuthorize("hasRole('ADMIN')")
    @DeleteMapping("/delete")
    public ResultVO deleteSupplier(@RequestBody GoodAgentDTO goodAgentDTO){
        return CommonUtil.ajaxReturn(goodsService.removeById(goodAgentDTO.getGId()));
    }
    @GetMapping("/count")
    public ResultVO countAgents(){
        return CommonUtil.ajaxReturn(agentService.countAgent());
    }
    @PostMapping("/all")
    public ResultVO getAllAgent(@RequestBody PageHelper pageHelper){
        return CommonUtil.ajaxReturn(agentService.getAllAgent(pageHelper));
    }
    @PostMapping("/allDesc")
    public ResultVO getAllAgentDesc(@RequestBody PageHelper pageHelper){
        return CommonUtil.ajaxReturn(agentService.getAllAgentDesc(pageHelper));
    }
    @PreAuthorize("hasRole('ADMIN')")
    @DeleteMapping("/deletePo")
    public ResultVO deletePo(@RequestBody AgentDTO agent){
        return CommonUtil.ajaxReturn(agentService.deleteAgent(agent));
    }
    @PreAuthorize("hasRole('ADMIN')")
    @DeleteMapping("/deletePoMulti")
    public ResultVO deletePoMulti(@RequestBody AgentDTO[] agent){
        try {
            for (AgentDTO agent1 : agent) {
                agentService.deleteAgent(agent1);
            }
            return CommonUtil.ajaxReturn("删除成功");
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return CommonUtil.ajaxReturn("删除失败");
    }
    @GetMapping("/search/{aName}")
    public ResultVO searchGood(@PathVariable String aName){
        return CommonUtil.ajaxReturn(agentService.searchAgent(aName));
    }
    @PutMapping("/updatePo")
    public ResultVO updateAgent(@RequestBody Agent agent){
        System.out.println(agent);
        return CommonUtil.ajaxReturn(agentService.updateAgentPo(agent));
    }
    @PostMapping("/insert")
    public ResultVO insertAgent(@RequestBody Agent agent){
        return CommonUtil.ajaxReturn(agentService.insertAgentPo(agent));
    }
}
